package edu.bupt.hbase;

public class ForTest1 {
    private static int count = 0;

    public int getRes(int[] flags, int n){
        count = 0;
        int[] s = new int[n+1];

        for(int i=1;i<=n;i++){
            s[i] = 1;
            dfs(flags, s, 1,i);
            s[i] = 0;//****
        }

        return count;
    }

    // pathN表示第一步，表示第2个数比第一个数大还是小；
    private void dfs(int[] flags, int[] s, int pathN, int lastNum){
        int n = flags.length;
        // 可行跳出；
        if (pathN == n){
            count++;
            return;
        }

        if (flags[pathN] == 0){
            //查看尚未
            for (int i=1;i<lastNum;i++){
                if (s[i]==0){
                    s[i]=1;
                    dfs(flags,s,pathN+1,i);
                    s[i]=0;
                }
            }
        }else {
            for (int i=lastNum+1;i<=n;i++){
                if (s[i]==0){
                    s[i]=1;
                    dfs(flags,s,pathN+1,i);
                    s[i]=0;
                }
            }
        }

    }


    public static void main(String[] args) {
        ForTest1 forTest1 = new ForTest1();
        int[] demo = new int[4];
        //demo[0] 闲置不用；
        demo[1] = 1;
        demo[2] = 1;
        demo[3] = 0;
        int s = forTest1.getRes(demo,4);
        System.out.println(s);
    }
}
